package com.zhu.commentstatisticspublisher.utils;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * 执行脚本 创建指定环境的脚本执行对象
 */

@NoArgsConstructor
@Data
public class ScriptCall<T> {


    public String scriptEnvironment;

    private int configNumTarget;

    public ScriptCall(String scriptEnvironment){
        this.scriptEnvironment = scriptEnvironment;
    }

    //shell
    public ArrayList<T> executeShellScript(String host,String shellScriptPath,ArrayList<String> parameters){
        return null;
    }

    //python
    public ArrayList<T> executePythonScript(String version,String pythonScriptPath, ArrayList<String> parameters)  {

        ArrayList<Integer> resultList = new ArrayList<>();
        //拼接成字符串数组
        if ("python3".equals(version)){
            configNumTarget = 2;
        }else if("shell".equals(version)){
            configNumTarget =1;
        }else {
            return null;
        }
        try {
            String[] envAndArgsConfig = new String[parameters.size() + configNumTarget];
            for (int i = 0; i < envAndArgsConfig.length; i++) { //拼接参列表
                if (i == 0) {
                    envAndArgsConfig[i] = scriptEnvironment;
                } else if (i == 1) {
                    envAndArgsConfig[i] = pythonScriptPath;
                } else {  // 2 = parameters.get( 2 - 2) = 0 d情感识别的参数列表只有1个参数 用户评论
                    envAndArgsConfig[i] = parameters.get(i - configNumTarget);
                }
            }
            System.out.println("参数列表是" + Arrays.toString(envAndArgsConfig));
            Process process = Runtime.getRuntime().exec(envAndArgsConfig);
            String valueLine = null;
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream(), "gb2312")); //防止中文乱码
            while ((valueLine = bufferedReader.readLine()) != null) {
                resultList.add(Integer.valueOf(valueLine));
            }
            if (process.waitFor() == 0){
                System.out.println("Successful user emotion recognition");
            }else {
                throw new RuntimeException("Script execution failed");
            }
            bufferedReader.close();
        }catch (InterruptedException | IOException e){
            e.printStackTrace();
        }
      //  System.out.println(resultList);
        return (ArrayList<T>) resultList;
    }


    /**
    public static void main(String[] args) throws IOException {
        //PySystemState sys = Py.getSystemState();
        //sys.path.add("D:\\java\\NLP\\venv\\Lib\\site-packages");
        ScriptCall<Integer> zhu = new ScriptCall<>("D:\\java\\NLP\\venv\\Scripts\\python.exe");
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add(" 这蛋糕真他妈的难吃啊,我太他妈难吃了,体验感真的太差啦 )/");
        zhu.executePythonScript("python3","D:\\java\\NLP\\CommentStatistics-Publisher\\src\\main\\resources\\script\\publisher.py",arrayList);
    //   Properties properties = new Properties();
        /*
      //  properties.load(new FileInputStream("D:\\java\\NLP\\CommentStatistics-Publisher\\src\\main\\resources\\script.properties"));
        System.out.println(properties.getProperty("script.environment"));
        ClassLoader classLoader = EmotionRecognitionServiceImpl.class.getClassLoader();
        InputStream inputStream = classLoader.getResourceAsStream("script.properties");
        properties.load(inputStream);
        System.out.println(properties.getProperty("script.environment"));
         */




}
